//@ts-ignore
import CryptoJS from 'crypto-js'
/**
 * AES 加密
 * @param word: 需要加密的文本
 * KEY: // 需要前后端保持一致
 * mode: ECB // 需要前后端保持一致
 * pad: Pkcs7 //前端 Pkcs7 对应 后端 Pkcs5
 */
 
export const AES_Encrypt = (plaintext: any, keyWord = 'XwKsGlMcdPMEhR1B') => {
  var key = CryptoJS.enc.Utf8.parse(keyWord)

  let ciphertext = CryptoJS.AES.encrypt(plaintext, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }).toString()
  return ciphertext
}
 
/**
 * AES 解密
 * @param jsonStr
 */
 const keyStr = '-mall4j-password' // 解密用的key
 export function encrypt(word:any) {
	const time = new Date().getTime()
	const key = CryptoJS.enc.Utf8.parse(keyStr)
	const srcs = CryptoJS.enc.Utf8.parse(time + word) // 加密方式: 时间戳 + 密文
	const encrypted = CryptoJS.AES.encrypt(srcs, key, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	})
	return encrypted.toString()
}
